package com.jkj.http.sys.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkj.http.controller.BaseController;
import com.jkj.http.entity.LayPadding;
import com.jkj.http.interceptor.AuthorizeChecked;
import com.jkj.http.sys.entity.SysLog;
import com.jkj.http.sys.entity.SysPermission;
import com.jkj.http.sys.service.SysLogService;
import com.jkj.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping(value = "/system/log")
public class LogController extends BaseController {
    @Autowired
    private SysLogService logService;

    /**
     * 日志列表画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index() {
        return new ModelAndView("system/log/index");
    }


    /**
     * 查询日志信息
     *
     * @param pageIndex
     * @param pageSize
     * @param queryDate
     * @param keyWord
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String index(int pageIndex, int pageSize, String queryDate, String keyWord) {
        Date limitDate = DateUtil.date();
        String today = DateUtil.format(limitDate, "yyyy-MM-dd");
        try {
            limitDate = DateUtil.parse(today);
        } catch (DateException e) {
        }
        if (!StrUtil.isEmpty(queryDate)) {
            if (queryDate.equals("7")) {
                limitDate = DateUtil.offsetDay(limitDate, -7);
            } else if (queryDate.equals("30")) {
                limitDate = DateUtil.offsetMonth(limitDate, -1);
            } else if (queryDate.equals("90")) {
                limitDate = DateUtil.offsetMonth(limitDate, -3);
            } else {
                try {
                    limitDate = DateUtil.parse(today);
                } catch (DateException e) {
                }
            }
        }
        int totalCount = logService.getTotalCount(limitDate, keyWord);

        PageHelper.startPage(pageIndex, pageSize);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<SysLog> list = logService.getList(limitDate, keyWord);
        PageInfo<SysLog> pageInfo = new PageInfo<>(list);
        List<SysLog> pageData = pageInfo.getList();
        for (SysLog log : pageData) {
            log.setStrCreateTime(DateUtil.format(log.getCreateTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
        }

        LayPadding<SysLog> result = new LayPadding<SysLog>();
        result.setResult(true);
        result.setMsg("success");
        result.setList(pageData);
        result.setCount(totalCount);
        return JsonUtils.toJSON(result);
    }

    /**
     * 删除日志画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/delete", method = {RequestMethod.GET})
    public ModelAndView delete() {
        return new ModelAndView("system/log/delete");
    }

    /**
     * 删除日志
     *
     * @param keepType
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String delete(String keepType) {
        Date keepDate = DateUtil.date();
        String today = DateUtil.format(keepDate, "yyyy-MM-dd");
        try {
            keepDate = DateUtil.parse(today);
        } catch (DateException e) {
        }
        if (!StrUtil.isEmpty(keepType)) {
            if (keepType.equals("7")) {
                keepDate = DateUtil.offsetDay(keepDate, -7);
            } else if (keepType.equals("30")) {
                keepDate = DateUtil.offsetMonth(keepDate, -1);
            } else if (keepType.equals("30")) {
                keepDate = DateUtil.offsetMonth(keepDate, -3);
            } else if (keepType.equals("0")) {
                keepDate = new Date();
            } else {
                try {
                    keepDate = DateUtil.parse(today);
                } catch (DateException e) {
                }
            }
            logService.delete(keepDate);
            return success();
        }
        return error();
    }
}
